<?php
/**
 * Created by PhpStorm.
 * User: liu85
 * Date: 2019/2/24
 * Time: 14:37
 */

namespace app\common\service;


use app\common\model\Product;

class ProductService
{
    public static function formatAdminProductList($list){
        if(empty($list)){
            return [];
        }
        $newList = [];
        foreach($list as $v){
            $newList[] = [
                'id' => $v['id'],
                'name' => $v['name'],
                'icon' => $v['icon'],
                'quota' => $v['quota'],
                'link' => $v['link'],
                'term' => $v['term'].'天',
                'loanTime' => $v['loanTime'].'分钟',
                'interestRate' => $v['interestRate'].'%',
                'clickCount' => $v['clickCount'],
                'created' => date('Y-m-d H:i:s', $v['created']),
                'isHot' => $v['isHot'],
                'isNew' => $v['isNew'],
                'sort' => $v['sort'],
            ];
        }
        return $newList;
    }

    /**
     * 批量获取产品信息
     * @param $productIds
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function batchProductByIds($productIds)
    {
        $map[] = ['id', 'in', $productIds];
        $model = new Product();
        $list = $model->where($map)->select();
        $newList = [];
        foreach ($productIds as $v){
            foreach($list as $value){
                if($value['id'] == $v){
                    $newList[$v] = $value;
                }
            }
        }
        return $newList;
    }

    public static function getProductMapping()
    {
        $model = new Product();
        $list = $model->where([])->column('id,name');
        return $list;
    }
}